package cn.auto.service;

import java.util.HashMap;
import java.util.List;

import cn.auto.common.model.SuiteCase;
import cn.auto.utility.Status;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class SuiteCaseService {
	
	SuiteCase sc= new SuiteCase();
	List<SuiteCase> infos=null;
	
	public HashMap<Object, Object> getSC(){
		infos=sc.searchSC();
		return Status.searchResultCode(infos);
		
	}
	
	public HashMap<Object, Object> getSC(String sid){
		infos=sc.searchSC(sid);
		return Status.searchResultCode(infos);
		
	}

	public String editSC(JSONObject eInfo) {
		JSONArray adds= (JSONArray) eInfo.get("add");
		JSONArray dels= (JSONArray) eInfo.get("del");
		String sid=eInfo.getString("suite");
		boolean isSuc=false;
		//if adds array is not empty,add the new case to suite
		if(adds.size()>0){
			for(int i=0;i<adds.size();i++){
				JSONObject addobj=adds.getJSONObject(i);
				Integer cid= (Integer) addobj.get("id");
				isSuc=sc.addSC(String.valueOf(cid), sid);
			}
		}
		//if dels array is not empty,delete the case from suite
		if(dels.size()>0){
			for(int i=0;i<dels.size();i++){
				JSONObject delobj=dels.getJSONObject(i);
				Integer id= (Integer) delobj.get("id");
				isSuc=sc.delSC(String.valueOf(id));
			}
		}
		return Status.booleanResultCode(isSuc);
	}
	
}
